package org.agile4j.activemq.java;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 消息的消费者(接受者)
 * Created by Administrator on 2016/7/15.
 */
public class JMSConsumer {
    //默认连接用户名
    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
    //默认连接密码
    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
    //默认连接地址
    private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;

    public static void main(String[] args) {
        ConnectionFactory connectionFactory; // 连接工厂
        Connection connection = null ; // 连接

        Session session ;   //会话 接受或发送消息的线程
        Destination destination ; // 消息的目的地

        MessageConsumer messageConsumer ; // 消息的消费者

        // 实例化连接工厂
        connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKEURL) ;

        try {
            // 通过连接工厂获取连接
            connection = connectionFactory.createConnection() ;
            // 启动连接
            connection.start();
            // 创建session
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE) ; // 不支持事务
            //创建一个连接HelloWorld的消息队列
            destination = session.createQueue("HelloWorld");
            //创建消息消费者
            messageConsumer = session.createConsumer(destination);

            while (true) {
                TextMessage textMessage = (TextMessage) messageConsumer.receive(100000);
                if (textMessage != null) {
                    System.out.println("收到消息: " + textMessage.getText());
                } else {
                    break;
                }
            }
        }catch (JMSException e) {
            e.printStackTrace();
        }

    }
}
